package com.utils
{
	import com.org.adobe.images.PNGEncoder;
	import com.org.greensock.TweenLite;
	import com.org.greensock.easing.Back;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.FileReference;
	import flash.text.TextField;
	
	public class VideoCamera extends Sprite
	{
		public static const instance:VideoCamera = new VideoCamera();
		private var _camera:Camera;
		private var _video:Video;
		private var _videoContain:Sprite;
		private var _photoContain:Sprite;
		private var _previewBitmap:Bitmap;
		private var _cameraBtn:ButtonModule;
		private var _leftBtn:ButtonModule;
		private var _rightBtn:ButtonModule;
		
		private var _bitmapData:BitmapData;
		private var _bitmap:Bitmap;
		private var _cameraInfoTxt:TextField;
		
		private var _cameraWidth:int;
		private var _cameraHeight:int;
		private var _fps:int = 26;
		private var _moveType:int = 0;
		
		private var _bitmapList:Array = [];
		private var _bitmapContainList:Array=[]; 
		public function VideoCamera()
		{
			if(instance) throw Error('不能重复实例化!');
		}
		
		/**启动函数*/		
		public function start():void
		{
			if(Camera.names.length <= 0)
			{
				trace("没有检测到摄像头!"); 
				return;
			} 
			initContain();
			initCamera();
			initUI();
		}
		
		private function initContain():void
		{
			if(!_videoContain) _videoContain = new Sprite();
			if(!_photoContain) _photoContain = new Sprite();
			if(!_previewBitmap) _previewBitmap = new Bitmap();
			this.addChild(_photoContain);
			this.addChild(_videoContain);
			this.addChild(_previewBitmap);
		}
		
		private function initUI():void
		{
			_cameraInfoTxt = TextFieldUtil.newTextField(_videoContain,'w:'+_cameraWidth+'\nh:' + _cameraHeight+'\nfps:'+_fps,null,'#FF0000');
			_cameraInfoTxt.width = _cameraInfoTxt.textWidth + 5;
			_cameraInfoTxt.x = _videoContain.width - _cameraInfoTxt.width;
			
			if(!_cameraBtn) _cameraBtn = new ButtonModule(null,['拍照'],null,{height:20},onPhotoHandler);
			_cameraBtn.x = _videoContain.x + _videoContain.width/2 - _cameraBtn.width/2;
			_cameraBtn.y = _videoContain.y + _videoContain.height+5;
			this.addChild(_cameraBtn);
			_leftBtn = new ButtonModule('assets/images/btnLeft.png');
			_leftBtn.y = stage.stageHeight - 68-5;
			this.addChild(_leftBtn);
			_rightBtn = new ButtonModule('assets/images/btnLeft.png');
			_rightBtn.scaleX = -1;
			_rightBtn.y = stage.stageHeight - 68-5;
			_rightBtn.x = stage.stageWidth;
			this.addChild(_rightBtn);
			_rightBtn.visible = _leftBtn.visible = false;
			_leftBtn.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			_rightBtn.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			_leftBtn.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			_rightBtn.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
		}
		
		protected function onMouseUpHandler(e:MouseEvent):void
		{
			_moveType = 0;
			this.removeEventListener(Event.ENTER_FRAME,onLoopHandler);
		}
		
		private function onMouseDownHandler(e:MouseEvent):void
		{
			e.target == _leftBtn ? _moveType = 2 :_moveType = 1;
			this.addEventListener(Event.ENTER_FRAME,onLoopHandler);
		}
		
		protected function onLoopHandler(event:Event):void
		{
			if(_moveType == 1){
				_photoContain.x -= 10;
				if(_photoContain.x <= stage.stageWidth - _photoContain.width ) {
					_photoContain.x = stage.stageWidth - _photoContain.width;
				}
			}else{
				_photoContain.x +=  10;
				if(_photoContain.x >=0) {
					_photoContain.x = 0;
				}
			}
		}
		
		private function initCamera():void
		{
			if(!_camera) _camera = Camera.getCamera();
			_cameraWidth = _camera.width * 2.3;
			_cameraHeight = _camera.height *2.3;
			_camera.setMode(_cameraWidth,_cameraHeight,_fps);
			if(!_video) _video = new Video(_cameraWidth,_cameraHeight);
			_video.attachCamera(_camera);
			_videoContain.addChild(_video);
			_videoContain.x = (stage.stageWidth - _videoContain.width)/2;
		}
		
		private function onPhotoHandler():void
		{
			_bitmapData = new BitmapData(_video.width,_video.height);
			_bitmapData.draw(_video);
			_bitmap = new Bitmap(_bitmapData);
			_bitmapList.push(_bitmap);
			_bitmap.name = 'image_' + LoaclTime.dateFormat('') + LoaclTime.timeFormat(true,'');
			var bitmapContain:BitmapContain = new BitmapContain();
			bitmapContain.addTarget(_bitmap,_photoContain,_previewBitmap,delFunc);
			_bitmapContainList.push(bitmapContain);
			_photoContain.y = stage.stageHeight - _photoContain.height;
			bitmapContain.x = (_bitmapList.length-1) * _bitmap.width;
			if(_photoContain.width > stage.stageWidth){
				_rightBtn.visible = _leftBtn.visible = true;
			}else{
				_rightBtn.visible = _leftBtn.visible = false;
			}
		}		
		
		private function delFunc(bmp:Bitmap,sp:Sprite):void
		{
			_bitmapList = getDelList(bmp,_bitmapList);
			_bitmapContainList = getDelList(sp,_bitmapContainList);
			for (var j:int = 0; j < _bitmapContainList.length; j++) 
			{
				_bitmapContainList[j].x = _bitmapContainList[j].width * j;
			}
			if(_photoContain.width <= stage.stageWidth){
				_rightBtn.visible = _leftBtn.visible = false;
			}
		}		
		
		private function getDelList(target:*,list:Array):Array{
			var tempList:Array = [];
			for (var i:int = 0; i < list.length; i++) 
			{
				if(list[i] == target){
					target.parent.removeChild(target);
					target = null;
				}else{
					tempList.push(list[i]);
				}
			}
			return tempList;
		}
		
		
		
	}
}
import com.org.adobe.images.PNGEncoder;
import com.utils.EffectUtil;
import com.utils.ITip;
import com.utils.TextFieldUtil;
import com.utils.Tips;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
import flash.net.FileReference;
import flash.text.TextField;
import flash.text.TextFieldType;

class BitmapContain extends Sprite{
	private var _target:Bitmap;
	private var _previewBitmap:Bitmap;
	private var _nameTxt:TextField;
	private var _saveTxt:TextField;
	private var _delTxt:TextField;
	private var _fileReference:FileReference;
	private var _deleFunc:Function;
	public function addTarget(target:Bitmap,parent:DisplayObjectContainer,previewBitmap:Bitmap,deleFunc:Function):void
	{
		_target = target;
		_target.scaleX = _target.scaleY = .3;
		_previewBitmap = previewBitmap;
		this.addChild(_target);
		parent.addChild(this);
		_deleFunc = deleFunc;
		this.addEventListener(MouseEvent.ROLL_OVER,onRollHandler);
		this.addEventListener(MouseEvent.ROLL_OUT,onRollHandler);
		this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromToStageHandler);
		_nameTxt = TextFieldUtil.newTextField(this,_target.name,{type:TextFieldType.DYNAMIC,mouseEnabled:false,width:100,wordWrap:true},'#FF0000',12);
		_saveTxt =  createTextButton(40,65,'保存',onSaveHandler);
		_delTxt =  createTextButton(75,65,'删除',onDelHandler);
		_nameTxt.height = _nameTxt.textHeight + 10;
		changeScale();
		_fileReference = new FileReference();
		this.buttonMode = true;
		_nameTxt.visible = _saveTxt.visible = _delTxt.visible = false;
	}
	
	private function onDelHandler(e:MouseEvent):void
	{
		_previewBitmap.bitmapData = null;
		_deleFunc(_target,this);
	}
		
	private function onRemoveFromToStageHandler(e:Event):void
	{
		this.removeEventListener(MouseEvent.CLICK,onSaveHandler);
		this.removeEventListener(MouseEvent.ROLL_OVER,onRollHandler);
		this.removeEventListener(MouseEvent.ROLL_OUT,onRollHandler);
		this.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromToStageHandler);
	}
	
	private function onSaveHandler(e:MouseEvent):void
	{
		_fileReference.save(PNGEncoder.encode(_target.bitmapData),_target.name+'.png');
	}
	
	private function onRollHandler(e:MouseEvent):void
	{
		if(e.type==MouseEvent.ROLL_OVER){
			_nameTxt.visible = _saveTxt.visible = _delTxt.visible = true;
			changeScale(true);
		}else{
			_nameTxt.visible = _saveTxt.visible = _delTxt.visible = false;
			changeScale();
		}
	}
	
	private function changeScale(change:Boolean = false):void{
		if(change){
			_previewBitmap.bitmapData = _target.bitmapData;
			_previewBitmap.x = (stage.stageWidth - _previewBitmap.width)/2;
			_previewBitmap.filters = [new GlowFilter(0xFF0000,1,6,6,2,1,true)];
		}else{
			_previewBitmap.filters = [];
			_previewBitmap.bitmapData = null;
		}
	}
	
	private function createTextButton( x:int, y:int, label:String, clickHandler:Function ):TextField
	{
		var button:TextField = new TextField();
		button.htmlText = "<font color='#FFFFFF'><u><b>" + label + "</b></u></font>";
		button.type = TextFieldType.DYNAMIC;
		button.selectable = false;
		button.width = button.textWidth + 5;
		button.height = button.textHeight +5;
		button.x = x;
		button.y = y;
		button.addEventListener( MouseEvent.CLICK, clickHandler );
		
		this.addChild( button );
		return button;
	} 
	
	
	
}